package com.lupengning.payment;

import com.lupengning.payment.entity.OrderInfo;
import com.lupengning.payment.entity.RefundInfo;
import com.lupengning.payment.service.OrderInfoService;
import com.lupengning.payment.service.RefundInfoService;
import com.lupengning.payment.service.WxPayService;
import java.util.List;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * @author lupengning
 * @version 1.0
 * @description:
 * @date 2022/1/16 0:07
 */
@Slf4j
@Component
public class task {

  @Resource
  private OrderInfoService orderInfoService;
  @Resource
  private WxPayService wxPayService;

  @Resource
  private RefundInfoService refundInfoService;
  /**
   * 从第0秒开始每隔30秒执行1次，查询创建超过5分钟，并且未支付的订单
   */
  @Scheduled(cron = "0/30 * * * * ?")
  public void orderConfirm() throws Exception {
    log.info("定时任务被执行......");
    List<OrderInfo> orderInfoList = orderInfoService.getNoPayOrderByDuration(5);
    for (OrderInfo orderInfo : orderInfoList) {
      String orderNo = orderInfo.getOrderNo();
      log.warn("超时订单 ===> {}", orderNo);
      //核实订单状态：调用微信支付查单接口
      wxPayService.checkOrderStatus(orderNo);
    }
  }

  /**
   * 从第0秒开始每隔30秒执行1次，查询创建超过5分钟，并且未成功的退款单
   */
  @Scheduled(cron =
      "0/30 * * * * ?")
  public void refundConfirm() throws Exception {
    log.info("refundConfirm 被执行...... ");
//找出申请退款超过5分钟并且未成功的退款单
        List<RefundInfo> refundInfoList = refundInfoService.getNoRefundOrderByDuration(5);
    for (RefundInfo refundInfo : refundInfoList) {
      String refundNo = refundInfo.getRefundNo();
      log.warn("超时未退款的退款单号 ===> {}", refundNo);
//核实订单状态：调用微信支付查询退款接口
      wxPayService.checkRefundStatus(refundNo);
    }
  }
}